<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true" align-center  draggable overflow>
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="50" ref="dialogForm">
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入" clearable type="textarea" :rows="5"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit(0)">保存</el-button>
        </template>
    </el-dialog>
</template>

<script>

export default {
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                purchase: '采购单备注',
                dye: '染色单备注',
                machining: '加工单备注',
                sale: '销售单备注',
            },
            visible: false,
            isSaveing: false,
            form: {
                id: "",
                remark: "",
            },
        }
    },
    mounted() {
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    var res = []
                    if(this.mode == 'purchase'){
                        res = await this.$API.business.purchase.order.remark.post(this.form);
                    }else if(this.mode == 'dye'){
                        res = await this.$API.business.dye.order.remark.post(this.form);
                    }else if(this.mode == 'machining') {
                        res = await this.$API.business.machining.order.remark.post(this.form);
                    }else {
                        res = await this.$API.business.sale.updateFields.post(this.form);
                    }
                    this.isSaveing = false;
                    if (res.code == 1) {
                        this.visible = false;
                        this.$emit('success', this.form, this.mode)
                        this.$message.success("操作成功")
                    } else {
                        this.$alert(res.message, "提示", {type: 'error'})
                    }
                }
            })
        },
        //表单注入数据
        async setData(data) {
            this.form.id = data.id
            this.form.remark = data.remark
        },
    }
}
</script>

<style lang="scss" scoped>
.flex{
    display: flex;
    justify-content: space-between;
}
</style>
